// 文件路径: src/main/java/cn/tedu/medicalmanagementsystem/manager/service/impl/UserServiceImpl.java
package cn.tedu.medicalmanagementsystem.manager.service.impl;

import cn.tedu.medicalmanagementsystem.base.exception.ServiceException;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.manager.mapper.ManagerUserMapper;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UserUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.User;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.UserVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Slf4j
@Service
public class ManagerUserServiceImpl implements ManagerUserService {

    @Autowired
    ManagerUserMapper managerUserMapper;

    // 添加用户
    @Override
    public UserVO add(UserAddParam userAddParam) {
        log.debug("开始处理添加用户业务，参数: {}", userAddParam);
        // 检查用户名是否已存在
        UserVO exituser = managerUserMapper.selectByUsername(userAddParam.getUsername());
        System.out.println("================================================数据库查找重名结果："+exituser);
        if (exituser != null) {
            throw new ServiceException(StatusCode.USERNAME_EXISTS);
        }
        // 将 DTO 转换为 Entity
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
//        user.setUsername(userAddParam.getUsername());
        user.setPassword("123456");
//        user.setRealName(userAddParam.getRealName());
//        user.setPhone(userAddParam.getPhone());
//        user.setEmail(userAddParam.getEmail());
//        user.setGender(userAddParam.getGender());
//        user.setIdCard(userAddParam.getIdCard());
//        user.setBirthday(userAddParam.getBirthday());
//        user.setAvatar(userAddParam.getAvatar());


        // 插入数据库
        managerUserMapper.add(user);
        // 查询插入后的用户信息（确保返回完整数据）
        System.out.println("================================================插入数据库的用户信息："+user);
        return managerUserMapper.selectByUsername(user.getUsername());
    }


    //    修改用户信息
    @Override
    public UserVO update(UserUpdateParam userUpdateParam) {
        log.debug("开始处理更新用户业务，参数: {}", userUpdateParam);
        // 检查用户是否存在 —— 使用 ID 查询
        UserVO existingUser = managerUserMapper.selectById(userUpdateParam.getId());
        if (existingUser == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        // 将 DTO 转换为 Entity
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        System.out.println("================================================修改数据库的用户信息："+user);
        // 更新数据库
        managerUserMapper.update(user);
        // 返回更新后的用户信息
        return managerUserMapper.selectByUsername(user.getUsername());
    }

    //    删除用户
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除用户业务，参数: {}", id);
        // 检查用户是否存在
        UserVO existingUser = managerUserMapper.selectById(id);
        if (existingUser == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        // 删除用户
        managerUserMapper.deleteById(id);
    }


    //    查询所有用户
    @Override
    public List<UserVO> allUsers() {
        return managerUserMapper.allUsers();
    }


    //    根据id查找用户
    @Override
    public UserVO selectById(Long id) {
        return managerUserMapper.selectById(id);
    }

    //    根据用户名查找用户
    @Override
    public UserVO selectByUsername(String username) {
        return managerUserMapper.selectByUsername(username);
    }

    @Override
    public List<UserVO> select(UserListQuery userListQuery) {
        log.debug("查询用户业务:userQuery={}",userListQuery);
        List<UserVO> list = managerUserMapper.select(userListQuery);
        return list;
    }


}
